package com.wye.util;

import com.wye.file.FastDFSFile;
import org.apache.http.NameValuePair;
import org.csource.common.MyException;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;

import javax.sound.midi.Track;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 实现FastDFS文件管理
 * 文件上传
 * 文件删除
 * 文件下载
 * 文件信息获取
 * Storage信息获取
 * Tracker信息获取
 */
public class FastDFSUtil {

    /**
     * 加载Tracker连接信息
     */
    static {
//        String fileName = "D:\\admin\\IdeaProjects\\wu-parent\\wu-service\\wu-service-file\\src\\main\\resources\\fdfs_client.conf";

        try {
            //查找classpath下的文件路径
            String path = new ClassPathResource("fdfs_client.conf").getPath();
            ClientGlobal.init(path);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
    }

    //文件上传
    public static String[] upload(FastDFSFile fastDFSFile) throws Exception{

        //附加参数

        //创建一个Tracker访问的客户端对象TrackerClient
        TrackerClient trackerClient = new TrackerClient();

        //通过TrackerClient访问TrackerServer服务，获取连接信息
        TrackerServer trackerServer = trackerClient.getConnection();

        //通过TrackerServer的连接信息可以获取Storage的连接信息，创建StorageClient对象储存Storage的连接信息
        StorageClient storageClient = new StorageClient(trackerServer, null);

        //通过StorageClient访问Storage，实现文件上传，并获取文件上传后的储存信息
        String[] uploads = storageClient.upload_file(fastDFSFile.getContent(), fastDFSFile.getExt(), null);
        return uploads;
    }

    /**
     * 获取文件信息
     * @param groupName
     * @param remoteFileName
     */
    public FileInfo getFileInfo(String groupName, String remoteFileName) throws IOException, MyException {
        //创建一个TrackerClient对象，通过TrackerClient对象访问TrackerServer
        TrackerClient trackerClient = new TrackerClient();
        //通过TrackerClient获取TrackerServer的连接信息
        TrackerServer trackerServer = trackerClient.getConnection();
        //通过TrackerServer获取Storage信息，创建StorageClient对象存储Storage信息
        StorageClient storageClient = new StorageClient(trackerServer, null);
        //获取文件信息
        return storageClient.get_file_info(groupName, remoteFileName);
    }

    /**
     * 文件下载
     */
    public static InputStream downFile(String groupName, String remoteFileName) throws IOException, MyException {
//        TrackerClient trackerClient = new TrackerClient();
//        TrackerServer trackerServer = trackerClient.getConnection();
//        StorageClient storageClient = new StorageClient(trackerServer, null);
        StorageClient storageClient = getStorage();
        byte[] buffer = storageClient.download_file(groupName, remoteFileName);
        return new ByteArrayInputStream(buffer);
    }

    public static void deleteFile(String groupName, String remoteFileName) throws IOException, MyException {
        StorageClient storageClient = getStorage();
        storageClient.delete_file(groupName, remoteFileName);

    }

    /**
     * 获取Storage信息
     * @return
     * @throws IOException
     */
    public static StorageServer getStorageServer() throws IOException {
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getConnection();
        return trackerClient.getStoreStorage(trackerServer);
    }

    /**
     * 获取Tracker信息
     * @return
     * @throws IOException
     */
    public static String getTrackerInfo() throws IOException {
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getConnection();
        //tracker的IP，HTTP端口
        String ip = trackerServer.getInetSocketAddress().getHostString();
        int tracker_http_port = ClientGlobal.getG_tracker_http_port();
        String url = "http://" + ip + ":" + tracker_http_port;
        return url;
    }

    private static StorageClient getStorage() throws IOException {
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getConnection();
        StorageClient storageClient = new StorageClient(trackerServer, null);
        return storageClient;
    }


}
